package com.dodo.blog.ui.ajax;

import com.dodo.blog.RequestCycle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
public class RepeaterAjaxRequestHandler
        implements IAjaxRequestHandler
{

    private static final long serialVersionUID = 1L;

    @Override
    public void handle() throws Exception
    {
        HttpServletRequest httpRequest = RequestCycle.get().getRequest();
        HttpServletResponse httpResponse = RequestCycle.get().getResponse();

        String listenerId = httpRequest.getParameter( AjaxListener.PARAM_ID );
        if ( listenerId != null )
        {
            AjaxListener listener = ( AjaxListener ) httpRequest.getSession().getAttribute( listenerId );
            if ( listener != null )
            {
                httpResponse.getWriter().write( listener.renderAjax( httpRequest ) );
            }
        }
    }
}
